package com.scalpel.websocket.server;

import com.scalpel.websocket.handler.ChildChannelHandler;
import com.scalpel.websocket.handler.MyWebSocketServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * Created by Administrator on 2017/9/27.
 */
public class NettyServer implements  Runnable {
    @Override
    public void run() {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workGroup = new NioEventLoopGroup();

        try {

            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workGroup);
            b.channel(NioServerSocketChannel.class).childHandler(new ChildChannelHandler());
//            b.childHandler(new MyWebSocketServerHandler());
            System.out.println("服务端开启等待客户端连接 ... ...");
            Channel ch = b.bind(7397).sync().channel();
            ch.closeFuture().sync();

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }
}
